package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.release.utils.FastReleaseUtils;
import com.uchengwang.ui.cm_ui.umsg.bean.SchoolClubBean;
import com.uchengwang.ui.utils.DateUtil;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.view.wheelView.view.TimePickerView;

import java.io.File;
import java.util.Date;

import me.shaohui.advancedluban.OnCompressListener;

/**
 * Created by U城网 on 2017/6/6.
 */

public class ClubAuthen2Activity extends AllBaseActivity implements View.OnClickListener{
    private EditText slogen, des;
    private Button ok;
    private TextView time;
    private ActivityTitleBar titleBar;
    private long createTime;//选择的时间
    private boolean isAllow = true;//防止多次提交
    private long orgAuthenId;//社团ID
    private String headUrl, clubName, headUrlOss, adminName, schoolName, slogenS, descS;
    private int clubType;//社团类型
    private int type;//根据type跳转不同的界面 0默认跳转到MainActivity  1跳转到社团活动报名页
    private String orgTypeName;//社团类型名
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_authen2);

        clubName = getIntent().getStringExtra(BaseConfig.ORG_NAME);
        adminName = getIntent().getStringExtra(BaseConfig.ADMIN_NAME);
        schoolName = getIntent().getStringExtra(BaseConfig.UNI_NAME);
        headUrl = getIntent().getStringExtra(BaseConfig.LOGO_IMG);
        orgAuthenId = getIntent().getLongExtra(BaseConfig.ORG_AUTH_ID, 0);
        clubType = getIntent().getIntExtra(BaseConfig.PAR_CLASSI_FY_ID, 0);
        type = getIntent().getIntExtra(BaseConfig.TYPE, 0);
        orgTypeName = getIntent().getStringExtra(BaseConfig.ORG_TYPE);

        initView();

        titleBar.setTitle("社团资料", 0);

    }

    private void initView(){
        titleBar = (ActivityTitleBar) findViewById(R.id.clubAuthen2_titleBar);

        time = (TextView) findViewById(R.id.clubAuthen2_time);
        time.setOnClickListener(this);
        slogen = (EditText) findViewById(R.id.clubAuthen2_slogen);
        des = (EditText) findViewById(R.id.clubAuthen2_des);
        ok = (Button) findViewById(R.id.clubAuthen2_ok);
        ok.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.clubAuthen2_time://选择时间
                //打开时间选择器
                FastReleaseUtils.getUtils().openTime(this, time,createTime, TimePickerView.Type.YEAR_MONTH_DAY, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date) {
                        createTime = date.getTime();
                        time.setText(DateUtil.getDateUtil().getDateByDate(DateUtil.DATE_FORMAT_YMD, date));
                    }
                });
                break;

            case R.id.clubAuthen2_ok://提交
                slogenS = slogen.getText().toString();
                descS = des.getText().toString();

                if (createTime ==0 ){
                    UCommonUtil.getUtil().showToast(this, "请选择社团创建时间");
                    return;
                }
                if (createTime > System.currentTimeMillis()){
                    UCommonUtil.getUtil().showToast(this, "无效的时间");
                    return;
                }
//                if (TextUtils.isEmpty(slogenS)){
//                    UCommonUtil.getUtil().showToast(this, "请填写社团的标语");
//                    return;
//                }
//                if (TextUtils.isEmpty(descS)){
//                    UCommonUtil.getUtil().showToast(this, "请简要介绍下自己的社团");
//                    return;
//                }

                if (!isAllow){
                    UCommonUtil.getUtil().showToast(this, "正在提交...");
                    return;
                }

                isAllow = false;
                LoadingUtils.getUtils().loading(this);
                compress();


                break;

        }
    }


    /**
     * 压缩图片在上传
     * */
    private void compress(){
        UCommonUtil.getUtil().compressImageAlone(this, 300, headUrl, new OnCompressListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(File file) {
                String head = file.getAbsolutePath();//压缩后的想地址

                //上传到阿里云
                OSSManager.getOSSManager().putHeadToOss(head, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        isAllow = true;
                        LoadingUtils.getUtils().hide();
                        UCommonUtil.getUtil().showToast(ClubAuthen2Activity.this, "图片上传失败");
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        //上传成功返回的oss的头像地址
                        headUrlOss = data;
                        putInfo();
                    }
                });

            }

            @Override
            public void onError(Throwable e) {
                isAllow = true;
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(ClubAuthen2Activity.this, "图片选择错误");
            }
        });
    }

    /**
     * 将信息提交到我们自己的服务器
     * */
    private void putInfo(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ORG_NAME, clubName);
        map.put(BaseConfig.ORG_SIGN, slogenS);
        map.put(BaseConfig.SUB_CLASSIFY_NAME, orgTypeName);//社团类型名
        map.put(BaseConfig.PAR_CLASSI_FY_ID, clubType);//社团类型
        map.put(BaseConfig.LOGO_IMG, headUrlOss);
        map.put(BaseConfig.BUILD_TIME, createTime);
        map.put(BaseConfig.UNI_NAME, UChengApplication.mySchoolName);
        map.put(BaseConfig.UNI_ID, UChengApplication.myUniId);
        map.put(BaseConfig.ORG_DESCRIPTION, descS);
        map.put(BaseConfig.ADMIN_NAME, adminName);
        map.put(BaseConfig.ORG_AUTH_ID, orgAuthenId);
        HttpManager.newInstance(this).query(ClubConfig.Club.method_create, ClubConfig.Club.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                isAllow = true;
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(ClubAuthen2Activity.this, "认证失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                if (status ==0 ){
                    UCommonUtil.getUtil().showToast(ClubAuthen2Activity.this, "认证成功");
//                    UClubClubFragment.isAuthenClub = true;

                    Intent intent = null;
                    switch (type){
                        case 0:
                            intent = new Intent(ClubAuthen2Activity.this, MainActivity.class);
                            break;
                        case 1://跳转到社团活动报名季
//                            intent = new Intent(ClubAuthen2Activity.this, ClubActApplyActivity.class);
                            if (!TextUtils.isEmpty(data)){
                                SchoolClubBean bean = JsonUtils.getManager().stringToBean(data, SchoolClubBean.class);
                                intent.putExtra(BaseConfig.BEAN2, bean);
                            }
                            break;
                    }
                    if (intent != null){
                        startActivity(intent);
                    }
                    ClubAuthen2Activity.this.finish();
                    return;
                }
                isAllow = true;
                UCommonUtil.getUtil().showToast(ClubAuthen2Activity.this, msg);
            }
        });
    }
}
